import abc
from typing import Generic, TypeVar

AnySample = TypeVar('AnySample')


class DatasetWriter(Generic[AnySample], abc.ABC):
    @abc.abstractmethod
    def write(self, sample: AnySample):
        raise NotImplementedError

    @abc.abstractmethod
    def flush(self):
        pass

    @abc.abstractmethod
    def close(self):
        pass

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()

    def __del__(self):
        self.close()
